using System;
using System.Collections.Specialized;
using System.IO;
using System.Web;
using M = System.Web.Mail;
using Dimok;
using Config = Dimok.Config;
using Dimok.Exceptions;
using Dimok.Images;

namespace Dimok {

	////////////////////////////////////////////////////////////////////////////////////////////////////
	/// <summary>	Summary description for Class1. </summary>
	///
	/// <remarks>	Dimon, 12/21/2010. </remarks>
	////////////////////////////////////////////////////////////////////////////////////////////////////

	public class Fax {
		#region HTML

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Mail Server will be taken from Config. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="html">			. </param>
		/// <param name="to">				. </param>
		/// <param name="from">			. </param>
		/// <param name="fax">			. </param>
		/// <param name="faxEMail">	. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static void HTML(string html,string to,string from,string fax,string faxEMail){
			HTML(html,to,from,fax,faxEMail,"");
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Mail Server will be taken from Config. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="html">				. </param>
		/// <param name="to">					. </param>
		/// <param name="from">				. </param>
		/// <param name="fax">				. </param>
		/// <param name="faxEMail">		. </param>
		/// <param name="mailServer">	default is taken from .config (if null or "" - local mail server) </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static void HTML(string html,string to,string from,string fax,string faxEMail,string mailServer){
			HTML(html,to,from,fax,faxEMail,mailServer,null,null);
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Mail Server will be taken from Config. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="html">					. </param>
		/// <param name="to">						. </param>
		/// <param name="from">					. </param>
		/// <param name="fax">					. </param>
		/// <param name="attachments">	. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static void HTML(string html,string to,string from,string fax,byte[][] attachments){
			HTML(html,to,from,fax,attachments,null);
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Mail Server will be taken from Config. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="html">				. </param>
		/// <param name="to">					. </param>
		/// <param name="from">				. </param>
		/// <param name="fax">				. </param>
		/// <param name="fileNames">	. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static void HTML(string html,string to,string from,string fax,string[] fileNames){
			HTML(html,to,from,fax,(byte[][])null,fileNames);
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Mail Server will be taken from Config. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="html">					. </param>
		/// <param name="to">						. </param>
		/// <param name="from">					. </param>
		/// <param name="fax">					. </param>
		/// <param name="attachments">	. </param>
		/// <param name="fileNames">		. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static void HTML(string html,string to,string from,string fax,byte[][] attachments,string[] fileNames){
			HTML(html,to,from,fax,Config.application.FaxEMail,attachments,fileNames);
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Mail Server will be taken from Config. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="html">					. </param>
		/// <param name="to">						. </param>
		/// <param name="from">					. </param>
		/// <param name="fax">					. </param>
		/// <param name="faxEMail">			. </param>
		/// <param name="attachments">	. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static void HTML(string html,string to,string from,string fax,string faxEMail,byte[][] attachments){
			HTML(html,to,from,fax,faxEMail,attachments,null);
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Mail Server will be taken from Config. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="html">					. </param>
		/// <param name="to">						. </param>
		/// <param name="from">					. </param>
		/// <param name="fax">					. </param>
		/// <param name="faxEMail">			. </param>
		/// <param name="mailServer">		default is taken from .config (if null or "" - local mail server) </param>
		/// <param name="attachments">	. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static void HTML(string html,string to,string from,string fax,string faxEMail,string mailServer,byte[][] attachments){
			HTML(html,to,from,fax,faxEMail,mailServer,attachments,null);
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Mail Server will be taken from Config. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="html">				. </param>
		/// <param name="to">					. </param>
		/// <param name="from">				. </param>
		/// <param name="fax">				. </param>
		/// <param name="faxEMail">		. </param>
		/// <param name="mailServer">	default is taken from .config (if null or "" - local mail server) </param>
		/// <param name="fileNames">	. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static void HTML(string html,string to,string from,string fax,string faxEMail,string mailServer,string[] fileNames){
			HTML(html,to,from,fax,faxEMail,mailServer,null,fileNames);
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Mail Server will be taken from Config. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="html">				. </param>
		/// <param name="to">					. </param>
		/// <param name="from">				. </param>
		/// <param name="fax">				. </param>
		/// <param name="faxEMail">		. </param>
		/// <param name="fileNames">	. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static void HTML(string html,string to,string from,string fax,string faxEMail,string[] fileNames){
			HTML(html,to,from,fax,faxEMail,(byte[][])null,fileNames);
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Mail Server will be taken from Config. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="html">					. </param>
		/// <param name="to">						. </param>
		/// <param name="from">					. </param>
		/// <param name="fax">					. </param>
		/// <param name="faxEMail">			. </param>
		/// <param name="attachments">	. </param>
		/// <param name="fileNames">		. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static void HTML(string html,string to,string from,string fax,string faxEMail,byte[][] attachments,string[] fileNames){
			HTML(html,to,from,fax,faxEMail,Config.application.MailServer,attachments,fileNames);
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Fax html using mail box. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <exception cref="Exception">	Thrown when exception. </exception>
		///
		/// <param name="html">					. </param>
		/// <param name="to">						Attached to fax name when goes to fax queue. </param>
		/// <param name="from">					. </param>
		/// <param name="fax">					. </param>
		/// <param name="faxEMail">			default is taken from .config. </param>
		/// <param name="mailServer">		default is taken from .config (if null or "" - local mail server) </param>
		/// <param name="attachments">	. </param>
		/// <param name="fileNames">		. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static void HTML(string html,string to,string from,string fax,string faxEMail,string mailServer,byte[][] attachments,string[] fileNames){
			try{
				string fileTemp = html == "" ? "" : FileManager.GetFileFromBytes(HttpUtility.UrlDecode(html),".htm",true);
				M.MailMessage msg = new M.MailMessage();
				msg.BodyFormat = M.MailFormat.Html;
				msg.From = from;
				msg.To = faxEMail;
				msg.Subject = "\""+to+"\"[fax:"+fax+"]";
				StringCollection scTempFiles = new StringCollection();
				if( fileTemp != "" )scTempFiles.Add(fileTemp);
				if( attachments != null )
					foreach( byte[] attachment in attachments )
						scTempFiles.Add(FileManager.GetFileFromBytes(attachment,"."+Enums.GetExtention(attachment),true));
				foreach( string attFile in scTempFiles )
					msg.Attachments.Add(new M.MailAttachment(attFile));
				if( fileNames != null )
					foreach( string file in fileNames )
						msg.Attachments.Add(new M.MailAttachment(file));
				if(mailServer != null && mailServer != "" )M.SmtpMail.SmtpServer = mailServer;
				M.SmtpMail.Send(msg);
				foreach( string delFile in scTempFiles )
					File.Delete(delFile);
			}catch(Exception exc){
				string[] aParams = new string[]{html,to,from,fax,faxEMail, mailServer};
				throw new Exception("Params:"+string.Join(",",aParams),exc);
			}
		}
		#endregion HTML

		#region Frame

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Frames. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="frameSrc">	The frame source. </param>
		/// <param name="fax">			. </param>
		/// <param name="from">			. </param>
		/// <param name="to">				. </param>
		/// <param name="body">			A variable-length parameters list containing body. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static void Frame(string frameSrc,string fax,string from,string to,params object[] body){
			Frame(frameSrc,fax,Config.application.FaxEMail,from,to,body);
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Frames. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="frameSrc">	The frame source. </param>
		/// <param name="fax">			. </param>
		/// <param name="faxEMail">	. </param>
		/// <param name="from">			. </param>
		/// <param name="to">				. </param>
		/// <param name="body">			A variable-length parameters list containing body. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static void Frame(string frameSrc,string fax,string faxEMail,string from,string to,params object[] body){
			M.MailMessage m = new M.MailMessage();
			m.From = from;
			m.To = faxEMail;
			m.Subject = to+"[fax:"+fax+"]";
			string fileName = MakeFaxFile(frameSrc);
			m.Attachments.Add(new M.MailAttachment(fileName));
			foreach(string b in body )
				m.Body = b;
			try{
				if( Config.application.MailServer != "" )
					M.SmtpMail.SmtpServer = Config.application.MailServer;
				M.SmtpMail.Send(m);
			}finally{
				File.Delete(fileName);
			}
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Makes a fax file. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="frameSrc">	The frame source. </param>
		///
		/// <returns>	. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		private static string MakeFaxFile(string frameSrc){
			string text = "<FRAMESET><FRAME src='"+frameSrc+"'></FRAMESET>";
			return FileManager.GetFileFromBytes(text,".htm",true);
		}
		#endregion FaxFrame
	
	}
}
